<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');
RClassesImporter::cls('dbo.dtos.MemberDTO');
/**
 * The controller for State application
 *
 */
require_once 'class.phpmailer.php';
require_once 'class.smtp.php';
require_once 'class.pop3.php';
define('GUSER', 'admin@kbbv-bretoninvietnam.com');          // Email
define('GPWD', 'kbbv2012');                 // Password

class MemberFrontController extends AbstractBackEndController {

	var $_model;

	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('MemberModel');
		$this->_model = new MemberModel();
	}

	function display() {
		unset($_SESSION['email']);
		unset($_SESSION['username']);

		$username1 = SysLanguage::_('USERNAME');
		$password1 = SysLanguage::_('PASSWORD');
		$confirm1 = SysLanguage::_('CONFIRM');
		$firstname1 = SysLanguage::_('FIRSTNAME');
		$lastname1 = SysLanguage::_('LASTNAME');
		$place_birth = SysLanguage::_('PLACE_BIRTH');
		$birthday = SysLanguage::_('BIRTHDAY');
		$mobile = SysLanguage::_('MOBILE');
		$lemail = SysLanguage::_('EMAIL');
		$skype = SysLanguage::_('SKYPE');
		$current_address = SysLanguage::_('CURRENT_ADDRESS');
		$city = SysLanguage::_('CITY');
		$country = SysLanguage::_('COUNTRY');
		$zipcode = SysLanguage::_('ZIPCODE');
		$invol = SysLanguage::_('INVOL');
		$board = SysLanguage::_('BOARD');
		$active_member = SysLanguage::_('ACTIVE_MEMBER');
		$intent = SysLanguage::_('INTENT');
		$get_news = SysLanguage::_('GET_NEWS');
		$entrep = SysLanguage::_('ENTREP');
		$employee = SysLanguage::_('EMPLOYEE');
		$find_job = SysLanguage::_('FIND_JOB');
		$retired = SysLanguage::_('RETIRED');
		$company = SysLanguage::_('COMPANY');
		$position = SysLanguage::_('POSITION');
		$industry = SysLanguage::_('INDUSTRY');
		$employ_address = SysLanguage::_('EMPLOYEE_ADDRESS');
		$how_long = SysLanguage::_('HOW_LONG');
		$by_member = SysLanguage::_('BY_MEMBER');
		$medias = SysLanguage::_('MEDIAS');
		$internet = SysLanguage::_('INTERNET');
		$newspapers = SysLanguage::_('NEWSPAPERS');
		$events_concerts = SysLanguage::_('EVENTS_CONCERTS');
		$contact_friends = SysLanguage::_('CONTACT_FRIENDS');

		$option = "";
		for($i = 2012; $i>1900 ; $i--){
			$option .= '<option value='.$i.'>'.$i.'</option>';
		}



		$lang = $_REQUEST['lang'];
		$root = SYS_ROOT_URL;
		$model = $this->_model;
		$name = $_REQUEST['name'];
		$email = $_REQUEST['email'];
		$subject = $_REQUEST['subject'];
		$content = $_REQUEST['content'];
		$memberDTO = new MemberDTO();
		$team = SysLanguage::_("TEAM");
		$findus = SysLanguage::_("FINDUS");
		$subc = SysLanguage::_("SUBC");
		$null = SysLanguage::_("NULL");
		
		
		if ($_POST['register']){
			$memberDTO->bind($_POST);
			//$member = new MemberDTO();
			$memberDTO->username = $_POST['username1'];
			$memberDTO->email = $_POST['email'];

			$day = $_POST['day'];
			$month = $_POST['month'];
			$year = $_POST['year'];
			$arr = array($day, $month, $year);
			$birth = implode('/', $arr);
			$memberDTO->birthday = $birth;
			
			$username = $_POST['username1'];
			$password = $_POST['password1'];
			$repassword = $_POST['repassword'];
			$firstname = $_POST['firstname'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];
			$memberDTO->password = $password;

			$_SESSION['email'] = $_POST['email'];
			$_SESSION['firstname'] = $_POST['firstname'];
			$_SESSION['lastname'] = $_POST['lastname'];
			$_SESSION['username'] = $_POST['username1'];
			$_SESSION['address'] = $_POST['address'];
			$_SESSION['password'] = $_POST['password1'];

			$email = $_POST['email'];
			

			if($memberDTO->checkExisting('username', true) || $memberDTO->checkExisting('email', TRUE)) {
				
				if($_REQUEST['lang'] == 'fr'){
					$warning = "Ce speudo existe déjà.";
				} else if($_REQUEST['lang'] == 'en'){

					$warning = "This username or email exists already.";
				}
			}
			else {
				
				$result = $model->saveMemberDTO($memberDTO);
				$memberDTO->resetToNull();
				RubikHelper::debugPrePrint($result);
				if ($result){

					$this->setRedirectToFunction('confirm');
				}
			}
		}

		//Load title------------
		$sql = "SELECT * FROM title WHERE id=8";
		$resultTitle = SysDatabase::loadObject($sql);
		if($_REQUEST['lang'] == 'fr'){
			$title = $resultTitle->title_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$title = $resultTitle->title_en;
		}
		//Lopad description
		$sql = "SELECT * FROM settings WHERE id=11";
		$resultDesc = SysDatabase::loadObject($sql);
		if($_REQUEST['lang'] == 'fr'){
			$description = $resultDesc->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$description = $resultDesc->content_en;
		}

		$app_info = SysLanguage::_('APP_INFO');
		$pro_info = SysLanguage::_('PRO_INFO');
		$how = SysLanguage::_('HOW');
		$cancel = SysLanguage::_('CANCEL');
		$register = SysLanguage::_('REGISTER');
		eval('echo "'.SysApplication::loadTemplate('front/default').'";');
	}
	function paypalCancel(){
		$lang = $_REQUEST['lang'];
		$root = SYS_ROOT_URL;
		$mailbody = "
			Hi ".$_SESSION['firstname']."!
			Congratulations!You've just created an account in KBBV system.
			Your account infomation:
			Username: ".$_SESSION['username']."
			Password: ".$_SESSION['password']."
			
			Sincerely,
			KBBV.
		";

		$team = SysLanguage::_("TEAM");
		$findus = SysLanguage::_("FINDUS");
		$subc = SysLanguage::_("SUBC");
		eval('echo "'.SysApplication::loadTemplate('front/paypalCancel').'";');
	}
	function paypalSuccess(){
		$lang = $_REQUEST['lang'];
		$root = SYS_ROOT_URL;
		$sql = "UPDATE `kbbv`.`member` SET `active` = '1' WHERE `member`.`username` = '".$_SESSION['username']."'";
		SysDatabase::execute($sql);
		$username = $_SESSION['username'];
		$password = $_SESSION['password'];
		$firstname = $_SESSION['firstname'];
		$lastname = $_SESSION['lastname'];
		$email = $_SESSION['email'];
		$address = $_SESSION['address'];
		$mailbody = "
			Hi ".$_SESSION['firstname']."!
			Congratulations!You've just created an account in KBBV system.
			Your account infomation:
			Username: ".$_SESSION['username']."
			Password: ".$_SESSION['password']."
			
			Your account has been activate.
			Sincerely,
			KBBV.
		";
		$this->smtpmailer($_SESSION['email'], GUSER, "Support from KBBV!!!", "Email register from KBBV!!!", $mailbody);
		$team = SysLanguage::_("TEAM");
		$findus = SysLanguage::_("FINDUS");
		$subc = SysLanguage::_("SUBC");
		eval('echo "'.SysApplication::loadTemplate('front/paypalSuccess').'";');
	}

	function success(){
		$lang = $_REQUEST['lang'];
		$root = SYS_ROOT_URL;
		$username = $_SESSION['username'];
		$des = SysLanguage::_('DESC');
		$con = SysLanguage::_('CON');
		$back = SysLanguage::_("BACK");




		$team = SysLanguage::_("TEAM");
		$findus = SysLanguage::_("FINDUS");
		$subc = SysLanguage::_("SUBC");

		eval('echo "'.SysApplication::loadTemplate('front/success').'";');
	}

	function confirm(){
		$lang = $_REQUEST['lang'];
		$username = SysLanguage::_('USERNAME');
		$address1 = SysLanguage::_('ADDRESS');
		$price_pay = SysLanguage::_('PRICE_PAY');
		$firstname1 = SysLanguage::_('FIRSTNAME');
		$lastname1 = SysLanguage::_('LASTNAME');

		$payment = SysLanguage::_('PAYMENT');
		$choose_pay = SysLanguage::_('CHOOSE_PAY');
		$pay_bank = SysLanguage::_('PAY_BANK');
		$pay_paypal = SysLanguage::_('PAY_PAYPAL');
		$confirm_order = SysLanguage::_('CONFIRM_ORDER');

		$root = SYS_ROOT_URL;
		if (empty($_SESSION['username']) || empty($_SESSION['email'])){
			$this->setRedirect($root);
		}

		$sqlAccountPaypal = "SELECT * FROM settings WHERE id = 7	";
		$resultAccountPaypal = SysDatabase::loadObject($sqlAccountPaypal);
		if($resultAccountPaypal->content_fr != null){
			$accountPaypal = $resultAccountPaypal->content_en;
		} else {
			$accountPaypal = $resultAccountPaypal->content_fr;
		}

		$team = SysLanguage::_("TEAM");
		$findus = SysLanguage::_("FINDUS");
		$subc = SysLanguage::_("SUBC");
		$sqlPrice = "SELECT * FROM settings WHERE id = 8	";
		$resultPrice = SysDatabase::loadObject($sqlPrice);
		if($_REQUEST['lang'] == 'fr'){
			$price = $resultPrice->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$price = $resultPrice->content_en;
		}

		$sqlBankTf = "SELECT * FROM settings WHERE id = 5";
		$resultBank = SysDatabase::loadObject($sqlBankTf);
		if($_REQUEST['lang'] == 'fr'){
			$descBank = $resultBank->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$descBank = $resultBank->content_en;
		}

		$sqlPaypal = "SELECT * FROM settings WHERE id = 6";
		$resultPaypal = SysDatabase::loadObject($sqlPaypal);
		if($_REQUEST['lang'] == 'fr'){
			$descPaypal = $resultPaypal->content_fr;
		} else if($_REQUEST['lang'] == 'en'){
			$descPaypal = $resultPaypal->content_en;
		}


		$username = $_SESSION['username'];
		$password = $_SESSION['password'];
		$firstname = $_SESSION['firstname'];
		$lastname = $_SESSION['lastname'];
		$email = $_SESSION['email'];
		$address = $_SESSION['address'];
		$_SESSION['descBank'] = $descBank;

		$mailbody = "
				Hi ".$_SESSION['firstname']."!
				Congratulations! We are pleased to welcome you into our KBBV communauty.
				
				Your account details:
				Username: ".$_SESSION['username']."
				Password: ".$_SESSION['password']."
				 
				NB: Please confirm us by email to activate your account in order to process your payment. We
                will send you personally our account details.
				 
				 
				Sincerely yours,
				
				KBBV.
			
			";

		$this->smtpmailer($_SESSION['email'], GUSER, "Support from KBBV.", "Confirmation email registration of KBBV", $mailbody);

		eval('echo "'.SysApplication::loadTemplate('front/confirm').'";');
	}

	function smtpmailer($to, $from, $from_name, $subject, $body)
	{
		global $message;
		$mail = new PHPMailer();                  // tạo một đối tượng mới từ class PHPMailer
		$mail->IsSMTP();                         // bật chức năng SMTP
		$mail->SMTPDebug = 0;                      // kiểm tra lỗi : 1 là  hiển thị lỗi và thông báo cho ta biết, 2 = chỉ thông báo lỗi
		$mail->SMTPAuth = true;                  // bật chức năng đăng nhập vào SMTP này
		$mail->SMTPSecure = 'ssl';                 // sử dụng giao thức SSL vì gmail bắt buộc dùng cái này
		$mail->Host = 'mail.kbbv-bretoninvietnam.com';         // smtp của gmail
		$mail->Port = 465;                         // port của smpt gmail
		$mail->Username = GUSER;
		$mail->Password = GPWD;
		$mail->SetFrom($from, $from_name);
		$mail->Subject = $subject;

		$mail->Body = $body;
		$mail->AddAddress($to);
		$result = $mail->Send();

		if(!$result)
		{
			return $message = 'Send mail error: '.$mail->ErrorInfo;
			return false;
		} else {
			//$this->setRedirectToFunction('confirm');
			//return $message = 'Your mail has been sent!';
			//session_unset();

			return true;
		}
	}
}
